//
//  DownLoadViewController.m
//  MapleLeaf
//
//  Created by WangZe on 15/11/28.
//  Copyright © 2015年 AntennaBabyTeam. All rights reserved.
//

#import "DownLoadViewController.h"
#import "ZGSCartoonDetailCell.h"
#import "ZGSReadModel.h"
#import "DownLoadDetailViewController.h"
@interface DownLoadViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic, strong)UIView *downView;
@property(nonatomic, strong)UIButton *selectButton;
@property(nonatomic, strong)UIButton *downloadButton;
@property(nonatomic, strong)UILabel *selectedLabel;
@property(nonatomic, assign)NSInteger numberOfSelected;
@property(nonatomic, copy)NSString *sizeOfSelected;
@property(nonatomic, strong)NSMutableArray *sizeArr;
@property(nonatomic, strong)NSMutableArray *cartoonArr;
@property(nonatomic, strong)NSMutableDictionary *tempDic;
@property(nonatomic, strong)NSMutableArray *downloadArr;
@end

@implementation DownLoadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"下载";
    self.sizeArr = [NSMutableArray array];
    self.cartoonArr = [NSMutableArray array];
    self.tempDic = [NSMutableDictionary dictionary];
    self.downloadArr = [NSMutableArray array];
    [self.view addSubview:self.collectionView];
    [self createView];
}


- (void)createView
{
    self.numberOfSelected = 0;
    self.downView.hidden = NO;
    self.navigationController.navigationBar.translucent = YES;
}

- (UIView *)downView
{
    if (!_downView) {
        _downView = [[UIView alloc] initWithFrame:CGRectMake(- 2, HEIGHT - 60 , WIDTH + 4, 60)];
        _downView.layer.borderWidth = 1;
        _downView.layer.borderColor = MyBorderColor;
        
        [self.view addSubview:self.downView];
        [self.downView addSubview:self.selectButton];
        [self.downView addSubview:self.selectedLabel];
        [self.downView addSubview:self.downloadButton];
    }
    return _downView;
}
- (UILabel *)selectedLabel
{
    if (!_selectedLabel) {
        _selectedLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 + WIDTH / 5, 10, WIDTH / 5 * 3 - 15, 40)];
        _selectedLabel.font = MyFont(10);
        _selectedLabel.text = [NSString stringWithFormat:@"共选%ld话,共%@M", self.numberOfSelected, self.sizeOfSelected];
    }
    return _selectedLabel;
}
- (UIButton *)downloadButton
{
    if (!_downloadButton) {
        _downloadButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _downloadButton.frame = CGRectMake(WIDTH / 5 * 4 + 5, 10, WIDTH / 5 - 10, 40);
        _downloadButton.layer.borderColor = MyBorderColor;
        _downloadButton.layer.borderWidth = 1.5;
        [_downloadButton setTitle:@"下载" forState:UIControlStateNormal];
        [_downloadButton addTarget:self action:@selector(downloadButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _downloadButton;
}
- (void)downloadButtonAction:(UIButton *)button
{
    if (self.sizeArr.count == 0) {
        UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先选择下载内容" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
        [view show];
    }
    else
    {
        
        UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"提示" message:@"开始下载,请等待" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
        [view show];
        
        
        DownLoadDetailViewController *VC = [[DownLoadDetailViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
        [self.tempDic setObject:self.name forKey:@"name"];
        [self.tempDic setObject:self.picStr forKey:@"pic"];
        [self.tempDic setObject:[NSString stringWithFormat:@"下载 : (%@话 / %@话)", [NSString stringWithFormat:@"%ld", self.numberOfSelected], self.number]  forKey:@"number"];
        [self.cartoonArr removeAllObjects];
        [self.cartoonArr addObject:self.tempDic];
        VC.dataArr = self.cartoonArr;
        
        
       
        
        
        
    }
}
- (UIButton *)selectButton
{
    if (!_selectButton) {
        _selectButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _selectButton.frame = CGRectMake(10, 10, WIDTH / 5, 40);
        _selectButton.layer.borderColor = MyBorderColor;
        _selectButton.layer.borderWidth = 1.5;
        [_selectButton setTitle:@"全选" forState:UIControlStateNormal];
        [_selectButton addTarget:self action:@selector(selectedAllCartoon:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectButton;
}
- (void)selectedAllCartoon:(UIButton *)button
{
    
}
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.flowLayout.itemSize = CGSizeMake(WIDTH / 5, HEIGHT / 20);
        self.flowLayout.minimumInteritemSpacing = (WIDTH / 5 - 10) / 5;
        self.flowLayout.minimumLineSpacing = (WIDTH / 5 - 10) / 5;
        self.flowLayout.sectionInset = UIEdgeInsetsMake(WIDTH / 5 / 5 + 20, (WIDTH / 5 - 10) / 5 + 4, WIDTH / 5 / 5, (WIDTH / 5 - 10) / 5 + 4);
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:self.flowLayout];
        [self.collectionView registerClass:[ZGSCartoonDetailCell class] forCellWithReuseIdentifier:@"reuse"];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZGSCartoonDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    NSInteger number = self.dataArr.count - indexPath.row;
    cell.numberLable.text = [NSString stringWithFormat:@"%ld", number];
    cell.numberLable.layer.cornerRadius = 15;

    // 判断是否收费
    
    ZGSReadModel *model = self.dataArr[self.dataArr.count - indexPath.item - 1];
                           
    if ([model.unLock integerValue] == 0) {
        cell.numberLable.textColor = [UIColor redColor];
    }
    else {
        cell.numberLable.textColor = [UIColor blackColor];
    }
    
    
    return cell;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    self.downloadArr = [NSMutableArray array];
    ZGSCartoonDetailCell *cell = (ZGSCartoonDetailCell *)[collectionView cellForItemAtIndexPath:indexPath];
    ZGSReadModel *model = self.dataArr[self.dataArr.count - indexPath.item - 1];
//    cell.numberLable.layer.borderColor = [UIColor blueColor].CGColor;
    NSNumber *result = nil;
    NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
    result=[f numberFromString:model.size];
    if (cell.numberLable.isClick == NO) {
        self.numberOfSelected++;
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:result forKey:@"size"];
        [dic setObject:model.name forKey:@"name"];
        [dic setObject:model.url forKey:@"download"];
        [dic setObject:[NSString stringWithFormat:@" 第%ld话",  self.dataArr.count - indexPath.item - 1] forKey:@"num"];
        [self.sizeArr addObject:dic];
        [self.tempDic setObject:self.sizeArr forKey:@"detail"];
        
        
        CGFloat h = 0;
        for (NSMutableDictionary *dic in self.sizeArr) {
            NSNumber *temp = dic[@"size"];
            h = [temp floatValue] + h;
        }
        self.sizeOfSelected = [NSString stringWithFormat:@"%.2f", h * 1.0 / 1024 / 1024];
        cell.numberLable.isClick = YES;
        
    }
    else
    {
        self.numberOfSelected--;
        NSMutableArray *tempArr = [NSMutableArray arrayWithArray:self.sizeArr];
        for (NSMutableDictionary *dic in tempArr) {
            if (model.name == dic[@"name"]) {
                [self.sizeArr removeObject:dic];
            }
        }
        [self.downloadArr removeObject:model.url];
        CGFloat h = 0;
        for (NSMutableDictionary *dic in self.sizeArr) {
            NSNumber *temp = dic[@"size"];
            h = [temp floatValue] + h;
        }
        self.sizeOfSelected = [NSString stringWithFormat:@"%.2f", h * 1.0 / 1024 / 1024];
        cell.numberLable.isClick = NO;
    }
    
    [self createValue];
}

- (void)createValue
{
    self.selectedLabel.text = [NSString stringWithFormat:@"共选%ld话,共%@M", self.numberOfSelected, self.sizeOfSelected];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
